package com.example.mntz.controller;

import com.example.mntz.model.*;
import com.example.mntz.service.MyInvestmentService;
import com.example.mntz.service.ZhService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Controller
@RequestMapping("/Investment")
public class InvestmentController {
    @Autowired
    MyInvestmentService service;
    @Autowired
    ZhService zhService;

    @RequestMapping("/MyInvestment")
    public String MyInvestment() {
        return "Investment-MyInvestment";
    }

    @RequestMapping("/NewInvestment")
    public String NewInvestment() {
        return "Investment-NewInvestment";
    }

    @RequestMapping("/GoJdInvestment")
    public ModelAndView GoJdInvestment(Integer lb, String code) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("lb", lb);
        modelAndView.addObject("code", code);
        modelAndView.setViewName("Investment-GoJdInvestment");
        return modelAndView;
    }

    @GetMapping("/SelectNewInvestment")
    public ResponseEntity<LayuiTable> NewInvestment(Integer limit, Integer page, HttpSession session) {
        int id = ((User) session.getAttribute("session_user")).getuId();
        LayuiTable<Zq> zqPageInfo = service.selectNewInvestment(limit, page, id);
        try {
            return ResponseEntity.status(200).body(zqPageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(400).body(zqPageInfo);
        }
    }

    @GetMapping("/SelectMyInvestment")
    public ResponseEntity<LayuiTable> SelectMyInvestment(Integer limit, Integer page, HttpSession session, Zq zq) {
        int id = ((User) session.getAttribute("session_user")).getuId();
        Zh zh = new Zh();
        zh.setuId(id);
        zq.setZh(zh);
        LayuiTable<Zq> zqPageInfo = service.selectMyInvestment(limit, page, zq);
        return ResponseEntity.status(200).body(zqPageInfo);
    }

    @GetMapping("/NewUserZh")
    public String newUserZh(Zh zh) {
        zhService.insertZh(zh);
        return "forward:/Investment/NewInvestment";
    }

    @GetMapping("/SelectCollection")
    public ResponseEntity<LayuiTable> SelectAllCollection(Integer limit, Integer page, HttpSession session, TzXk tzXk) {
        int id = ((User) session.getAttribute("session_user")).getuId();
        List tzxks = service.SelectCollection(limit, page, tzXk, id);
        LayuiTable layuiTable = new LayuiTable(tzxks);
        return ResponseEntity.status(200).body(layuiTable);
    }

    //admin 控制timelx主页面
    @RequestMapping("/adminHtml")
    public String adminHtml() {
        return "admin-zqList";
    }

    @ResponseBody
    @RequestMapping("/selectTimelxs")
    public LayuiTable selectTimelxs(Integer limit, Integer page, Timelx timelx) {
        return service.selectTimelxs(limit, page, timelx);
    }

    /*添加修改赛道*/
    @RequestMapping("/adminIUtimetz")
    public ModelAndView adminIUtimetz(Integer timeLb) {
        ModelAndView modelAndView = new ModelAndView("admin-addzq");
        Timelx u = new Timelx();
        String a = "添加赛道";
        if (timeLb != null) {
            u = service.selectTimelx(timeLb);
            a = "修改赛道";
        }
        modelAndView.addObject("legend", a);
        modelAndView.addObject("timelx", u);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/adminGoIUtimetz")
    public void adminGoIUtimetz(Timelx timelx) {
        service.insertTz(timelx);
    }

    @ResponseBody
    @RequestMapping("/deletetzsd")
    public void deletetzsd(Integer timeLb) {
        service.deletetzsd(timeLb);
    }

    @Transactional
    @ResponseBody
    @RequestMapping("/deletetzsds")
    public void deletetzsds(@RequestParam("timeLbs[]") List<Integer> timeLb) {
        for (Integer i : timeLb) {

            service.deletetzsd(i);
        }
    }

    //    周期子页面
    @RequestMapping("/selectzq")
    public ModelAndView selectzq(Integer timeLb) {
        ModelAndView modelAndView = new ModelAndView("admin-zqmxList");
        modelAndView.addObject("timeLb", timeLb);
        return modelAndView;
    }


    @ResponseBody
    @RequestMapping("/selectzqLists")
    public LayuiTable selectzqLists(Integer limit, Integer page, Timelx timelb) {
        return service.selectzqLists(limit, page, timelb);
    }
    @RequestMapping("/adminIUtimetzmx")
    public ModelAndView adminIUtimetzmx(Integer zqId) {
        ModelAndView modelAndView = new ModelAndView("admin-addzqmx");
        Zq u = new Zq();
        String a = "添加赛道";
        if (zqId != null) {
            u = service.selectZq(zqId);
            a = "修改赛道";
        }
        modelAndView.addObject("legend", a);
        modelAndView.addObject("zq", u);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/adminItimetzmx")
    public void adminItimetzmx(Integer timeLb) {
        service.adminItimetzmx(timeLb);
    }
    @ResponseBody
    @RequestMapping("/adminGoIUzq")
    public void adminGoIUzq(Zq zq) {
        service.insertZq(zq);
    }
    @RequestMapping("/deletetzmxsd")
    @ResponseBody
    @Transactional
    public void deletetzmxsd(Integer zqId){
        service.deletezq(zqId);
    }
    @RequestMapping("/deletetzmxsds")
    @ResponseBody
    @Transactional
    public void deletetzmxsds(@RequestParam("timeLbs[]") List<Integer> zqIds){
        for(Integer zqId :zqIds){
            service.deletezq(zqId);
        }
    }
}
